<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
    <form action="">
        <input type="text" name="username">
        <br>
        <input type="password" name="password">
        <br>
        <input type="password" name="repass">
        <br>
        <input type="email" name="email">
        <br>
        <input type="number" name="tel">
        <br>
        <input type="submit">
    </form>
</body>
<script>
    //获取cookie
    // console.log(document.cookie);

    document.querySelector('form').onsubmit = function(){
        var xhr = new XMLHttpRequest;
       
        xhr.open('post','/api/register')
        var username = document.querySelector('[name="username"]').value
        var password = document.querySelector('[name="password"]').value
        var email = document.querySelector('[name="email"]').value
        var tel = document.querySelector('[name="tel"]').value
        xhr.setRequestHeader('content-type','application/x-www-form-urlencoded')
        xhr.send(`username=${username}&password=${password}&email=${email}&tel=${tel}`)
        xhr.onload = function(){
            var res = xhr.responseText
            res = JSON.parse(res)
            var {code,msg} = res
            alert(msg)
            if(code === 1){ 
                location.href = './login.html'
            }
        }
        return false
    }
</script>
</html>